package com.homwee.factory;

import com.homwee.tvap.common.ap.ComSettingManager;
import com.homwee.tvap.common.srvfunc.FactorySetting;

import android.app.Activity;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import java.util.Timer;
import java.util.TimerTask;

import android.widget.TextView;
import android.widget.Toast;
import com.homwee.factory.R;

public class FactoryDialog extends Dialog implements OnClickListener {
	private Context mContext;
	private FactoryActivity factoryActivity =null;
	private Button okButton;
	private Button cancelButton;
	private TextView dialogText;
	public ComSettingManager mcomSettingManager;
	public FactorySetting factorySetting;
	public String mKeyEvent;
	public FactoryDialog(Context context,String keyEvent) {
    	super(context, R.style.Theme_FactorySettingDialog);
    	mContext= context;
    	Log.v("111","");
    	if (context instanceof Activity) {
			factoryActivity = (FactoryActivity)context;
		}
    	mContext =context;
    	mKeyEvent=keyEvent.toUpperCase();
	}

	public FactoryDialog(Context context, AttributeSet attrs) {
		super(context, R.style.Theme_FactorySettingDialog);
		mContext = context;
		if (context instanceof Activity) {
			factoryActivity = (FactoryActivity)context;
		}
		mContext =context;
	}

    
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		mcomSettingManager=ComSettingManager.getInstance(mContext);
		factorySetting = mcomSettingManager.getFactory();
        setContentView(R.layout.factory_dialog);
        initView();
       
    }
    
    
	public void  initView(){
		okButton = (Button)findViewById(R.id.okButton);
		okButton.setFocusable(true);
		okButton.requestFocus();
		cancelButton = (Button)findViewById(R.id.cancelButton);
		cancelButton.setOnClickListener(this);
		okButton.setOnClickListener(this);
		dialogText=(TextView) findViewById(R.id.factory_Message);
		if(mKeyEvent.equalsIgnoreCase("NOT_ALLKEYEXIST")){
			Log.d("CHFactory_FactoryDialog","NOT_ALLKEYEXIST");
			dialogText.setText(R.string.factory_message_not_allkeyexist);
		}
	}
  
	 public void onClick(View view) {
	        switch (view.getId()) {
	             case R.id.okButton:
	            	   dismiss();
	            	   FactoryToast toast = new FactoryToast(mContext);
	    		       toast.setDuration(500);
	    		       toast.setToastIcon(R.drawable.pic_eye);
	    		       toast.setMessage(mContext.getString(R.string.factory_system_reset));
	    		       toast.getTextView().getBackground().setAlpha(200);
	    		       toast.show();
	    		       
	            	   new Handler().postDelayed(new Runnable() {
						@Override
						public void run() {
							// TODO Auto-generated method stub
							switch(mKeyEvent){
								case "FACTORY_RESET" :
									Log.d("FactoryDialog","in Factory Reset");
									factorySetting.systemInit();
								break;
								case "INITIALZATION" :
									Log.d("FactoryDialog","in INITIALZATION");
									Toast.makeText(mContext,
											mContext.getString(R.string.factory_clean_data_wait),
											Toast.LENGTH_SHORT).show();
									// toast.setGravity(mPaddingRight, 0, 0);
//									Timer timer = new Timer();
//									TimerTask task = new TimerTask() {
//										public void run() {
//											factorySetting.cleanApp(false);
//										}
//									};
//									timer.schedule(task, 300);
									factorySetting.systemClearApp();
									break;
								case "NOT_ALLKEYEXIST":
									Log.d("FactoryDialog","in NOT_ALLKEYEXIST");
									factorySetting.systemInit();
									break;
							}

							//factorySetting.cleanApp(true);
						}
						}, 200);

	                  break;
	            case R.id.cancelButton:
	            	switch (mKeyEvent){
						case "NOT_ALLKEYEXIST":
							Intent myIntent = new Intent();
							myIntent.setComponent(new ComponentName(
									"com.homwee.factory",
									"com.homwee.factory.FactoryActivity"));
							myIntent.addFlags(myIntent.FLAG_ACTIVITY_NEW_TASK);
							mContext.startActivity(myIntent);
							break;
					}
                    cancel();
                    break;
	        }
	    }

}

